package vcp.server.deamontasks;

import java.util.List;
import java.util.TimerTask;

import org.joda.time.DateTime;

import vcp.entity.DayStatistics;
import vcp.entity.SubscriptionHistory;
import vcp.enums.SiteEnum;
import vcp.server.DAO.CarDAO;
import vcp.server.DAO.DayStatisticsDAO;
import vcp.server.DAO.SubscriptionDAO;
import vcp.server.DAO.SubscriptionHistoryDAO;
import vcp.server.interfaces.IServer;
import vcp.server.utility.Constants.TableNames;
import vcp.server.utility.SystemClock;

/**
 * 
 * @author VCP team
 *         <p>
 *         class that runs every end of day and runs statistics
 *         </p>
 */
public class EndDayStatisticsTask extends TimerTask
{
	private final IServer server;

	/**
	 * class constructor
	 * 
	 * @param serverDeamon
	 */
	public EndDayStatisticsTask ( IServer serverDeamon )
	{
		this.server = serverDeamon;
	}

	@Override
	public void run ()
	{
		synchronized (this.server.getDBLock())
		{
			System.out.println("EndDayStatisticsTask:  TrueTime:" + new DateTime() + "  SysTime:" + SystemClock.getClockInstance().getSystemDateTime());
			// prepare DAO's
			SubscriptionHistoryDAO subscriptionHistoryDAO = new SubscriptionHistoryDAO();
			SubscriptionDAO subscriptionDAO = new SubscriptionDAO();
			CarDAO carDAO = new CarDAO();
			DayStatisticsDAO dayStatisticsDAO = new DayStatisticsDAO();

			// get all subscription from history for today's date
			DateTime today = SystemClock.getClockInstance().getSystemDateTime();
			for ( SiteEnum siteEnum : SiteEnum.values() )
			{
				List <SubscriptionHistory> allTodaySubscriptions = subscriptionHistoryDAO.findAllSubscriptionsHistoryByLeaveDate(today, siteEnum);

				// fill dayStatistics entry
				DayStatistics dayStatistics = new DayStatistics();
				dayStatistics.setID(this.server.getGeneratedID(TableNames.DAY_STATISTICS));
				dayStatistics.setNumOfUsedOrders(allTodaySubscriptions.size());
				dayStatistics.setNumOfCanceledOrders(this.calculateNumOfCanceled(allTodaySubscriptions));
				dayStatistics.setNumOfSubscription(subscriptionDAO.findTotalNumOfSubscriptions(siteEnum));
				dayStatistics.setNumOfBussinessClass(carDAO.findTotalNumOfBussinessClass(siteEnum));
				dayStatistics.setNumOfParkingLates(this.calculateNumOfParkingLates(allTodaySubscriptions));
				dayStatistics.setDate(today);
				dayStatistics.setSiteEnum(siteEnum);
				// save to DB
				dayStatisticsDAO.save(dayStatistics);
			}
		}
	}

	/**
	 * Calculates how many of todays subscription came late
	 * 
	 * @param allTodaySubscriptions
	 * @return calculated number of parking lates
	 */
	private int calculateNumOfParkingLates ( List <SubscriptionHistory> allTodaySubscriptions )
	{
		int count = 0;
		for ( SubscriptionHistory subscriptionHistory : allTodaySubscriptions )
		{
			if ( subscriptionHistory.getArrivalDate().isAfter(subscriptionHistory.getStartDate()) )
			{
				count ++ ;
			}
		}
		return count;
	}

	/**
	 * Calculates how many of todays subscription were canceled
	 * 
	 * @param allTodaySubscriptions
	 * @return calculated number of canceled subscriptions
	 */
	private int calculateNumOfCanceled ( List <SubscriptionHistory> allTodaySubscriptions )
	{
		int calc = 0;
		for ( SubscriptionHistory subscriptionHistory : allTodaySubscriptions )
		{
			if ( subscriptionHistory.isCanceld() )
			{
				calc ++ ;
			}
		}
		return calc;
	}
}
